<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>发改局</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet"
        href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome Icons -->
  <link rel="stylesheet" href="../plugins/fontawesome-free/css/all.min.css">
  <!-- IonIcons -->
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../dist/css/adminlte.min.css">

  <link rel="stylesheet"
        href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome Icons -->
  <link rel="stylesheet" href="../../plugins/fontawesome-free/css/all.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../../dist/css/adminlte.min.css">
</head>

<body class="hold-transition sidebar-mini">
<div class="wrapper">

  <!-- 主内容容器 -->
  <div class="content-wrapper">
    <!-- 容器头标题 -->
    <div class="content-header">
      <div class="container-fluid">

        <div class="row mb-2">

          <div class="col-sm-6">
            <h1 class="m-0">修改用户</h1>
          </div>

          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">首页</a></li>
              <li class="breadcrumb-item active">修改用户</li>
            </ol>
          </div>

        </div>

      </div>

    </div>

    <!-- 主题内容正文 -->
    <div class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-6">

            <div class="card card-primary">
              <!-- 卡片头部 -->
              <div class="card-header">
                <h3 class="card-title">修改用户</h3>
              </div>

              <!-- 表单开始 -->
              <form>
                <div class="card-body">

                  <input type="hidden" id="userid" name="id" th:value="${#httpSession.getAttribute('user_id')}">

                  <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" disabled="disabled" class="form-control" id="username" value="username" th:value="${#httpSession.getAttribute('user_name')}"
                           placeholder="用户名">
                  </div>

                  <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" class="form-control" id="password" name="password"
                           placeholder="密码">
                  </div>

                  <div class="form-group">
                    <label for="repassword">确认密码</label>
                    <input type="password" class="form-control" id="repassword" name="repassword" placeholder="确认密码">
                  </div>

                  <div class="form-group">
                    <div class="form-group">
                      <label for="roleId">角色</label>
                      <select class="form-control" id="roleId" name="roleId">

                      </select>
                    </div>
                  </div>

                </div>
                <input type="hidden" id="deleted_id" name="deleted" value="0" />
                <!-- 添加按钮 -->
                <div class="card-footer">
                  <button type="button" class="btn btn-primary" id="save_btn">保存</button>
                </div>
              </form>

            </div>

          </div>

        </div>

        <div class="col-lg-6">
        </div>

      </div>

    </div>

  </div>


  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
  </aside>

  <!-- 版权信息 -->
  <footer class="main-footer">
    <strong>Copyright &copy; 2021 <a href="http://www.chengzhouyun.com/" target="_blank">ChengZhouYun</a>.</strong> All
    rights reserved.
    <div class="float-right d-none d-sm-inline-block">
      <b>Version</b> 1.0
    </div>
  </footer>

</div>

<!-- REQUIRED SCRIPTS -->
<!-- jQuery -->
<script src="../plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE -->
<script src="../dist/js/adminlte.js"></script>

<!-- OPTIONAL SCRIPTS -->
<script src="../plugins/chart.js/Chart.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../dist/js/demo.js"></script>
<!-- AdminLTE dashboard demo (This is only for demo purposes) -->
<script src="../dist/js/pages/dashboard3.js"></script>



<script src="../../plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="../../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE -->
<script src="../../dist/js/adminlte.js"></script>

<!-- OPTIONAL SCRIPTS -->
<script src="../../plugins/chart.js/Chart.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../../dist/js/demo.js"></script>
<!-- AdminLTE dashboard demo (This is only for demo purposes) -->
<script src="../../dist/js/pages/dashboard3.js"></script>
</body>

<script th:inline="javascript">

  $(function(){

    // 登录的角色是管理员的情况下在做请求
    const r_role_id = /*[[${#httpSession.getAttribute('role_id')}]]*/{};  //{}是默认值,/*[[${user}]]*/ 是传进去的值
    if (r_role_id == 1) {
      // 是管理员的情况下请求所有角色列表
      $.get("/role/role/list", function (data) {
        // 取用户下拉框
        var select = document.getElementById("roleId");
        // 情况下拉框中的option
        var arr_option = select.options;
        // 先清除在添加
        for (var i = arr_option.length - 1; i >= 0; i--) {
          arr_option.remove(i);
        }
        // 数据状态码为200通过
        if (data.statusCode == 200) {
          // 取到list数据集合
          var list = data.list;

          // forEach遍历
          list.forEach(function (item) {
            // 创建select中的option
            var option = document.createElement('option');
            // 赋值用户id给value
            option.value = item.id;
            // 赋值用户名给内容
            option.innerHTML = item.name;
            // 添加到select中
            select.append(option);

          })
        }
      })
    } else {
      // 不是管理员请求企业角色
      // 是管理员的情况下请求所有角色列表
      $.get("/role/role/2", function (data) {
        // 取用户下拉框
        var select = document.getElementById("roleId");
        // 情况下拉框中的option
        var arr_option = select.options;
        // 先清除在添加
        for (var i = arr_option.length - 1; i >= 0; i--) {
          arr_option.remove(i);
        }
        // 数据状态码为200通过
        if (data.statusCode == 200) {
          // 取到list数据集合
          var item = data.object;
          // 创建select中的option
          var option = document.createElement('option');
          // 赋值用户id给value
          option.value = item.id;
          // 赋值用户名给内容
          option.innerHTML = item.name;
          // 添加到select中
          select.append(option);

        }
      })
    }

    // 保存按钮事件
    $("#save_btn").on("click",function (event) {
        var formData = new FormData();
        formData.id = $("#userid").val();
        formData.username = $("#username").val();
        formData.repassword = $("#repassword").val();
        formData.password = $("#password").val();
        formData.roleId = $("#roleId").val();
        formData.deleted = $("#deleted_id").val();

      $.ajax({
        url: "/usr/user",
        type: "put",
        data: JSON.stringify(formData),
        contentType: "application/json",
        async: false,
        dataType: "json",
        success: function (res) {

          if (res.statusCode == 200) {
            alert(res.message);
          } else {
            alert(res.errorMsg);
          }
          // 重载页面
          location.reload()
        }
      })

    })

  })

</script>
</html>
